package app.mvc.view.mediators
{
	import app.mvc.Notifications;
	import app.mvc.model.VO.UIObjectVO;
	import app.mvc.view.layers.ObjectsLayer;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import utils.UtilsObjects;
	
	public class ObjectsViewMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "ObjectsViewMediator";
		
		public function ObjectsViewMediator(mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
		}

		
		override public function handleNotification(notification:INotification):void
		{
			// TODO Auto Generated method stub
			super.handleNotification(notification);
			
			switch(notification.getName())
			{
				case Notifications.CMD_CLEAR_OBJECTS:
					(viewComponent as ObjectsLayer).clearAll();
					break;
				case Notifications.TO_ENABLE_DRAGGING_DETECTOR:
					(viewComponent as ObjectsLayer).dropLayer.enableDraggingListener(true);
					break;
				case Notifications.TO_ADD_OBJECT:
					var vo:UIObjectVO = notification.getBody() as UIObjectVO;
					UtilsObjects.uiObjectVOs.push( vo );
					(viewComponent as ObjectsLayer).dropLayer.addObject( vo );
					break;
				case Notifications.TO_REMOVE_OBJECT:
					var deleArr:Array = notification.getBody() as Array;
					var len:int=deleArr.length;
					var vo:UIObjectVO = null;
					for(var i:int=0;i<len;++i)
					{
						UtilsObjects.deleteVoByUID(deleArr[i]);
					}
					(viewComponent as ObjectsLayer).dropLayer.removeObjects(deleArr);
					break;
				case Notifications.TO_MOVE_OBJECTS:
					var vo:UIObjectVO = null;
					var obj:Object = notification.getBody();
					var selectedObjects:Array = obj.selectedObjects;
					var len:int = selectedObjects.length;
					var uid:String = "";
					var data:UIObjectVO = null;
					for(var i:int=0;i<len;++i)
					{
						uid = selectedObjects[i];
						if(uid)
						{
							data = UtilsObjects.getUIVoByUID(uid);
							data.x += obj.changeX;
							data.y += obj.changeY;
							(viewComponent as ObjectsLayer).dropLayer.moveObject(data);
						}
					}
					
					break;
			}
		}
		
		override public function listNotificationInterests():Array
		{
			// TODO Auto Generated method stub
			return [Notifications.CMD_CLEAR_OBJECTS,
						Notifications.TO_ENABLE_DRAGGING_DETECTOR,
						Notifications.TO_ADD_OBJECT,
						Notifications.TO_REMOVE_OBJECT,
						Notifications.TO_MOVE_OBJECTS
			];
		}
		
	}
}